﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using Web.Resource;

namespace Web.Controllers
{
    /// <summary>
    /// 文件上传控制器    
    /// 作者：陈国伟 日期：2013/07/05
    /// </summary> 
    public class FileUploadController : Controller
    {
        /// <summary>
        ///文件上传模型
        /// </summary>
        public class Model
        {
            /// <summary>
            /// 回调函数名
            /// </summary>
            public string CallBack { get; set; }
            /// <summary>
            /// 允许的扩展名
            /// </summary>
            public string Ext { get; set; }
            /// <summary>
            /// 大小限制(MB)
            /// </summary>
            public int SizeLimit { get; set; }
            /// <summary>
            /// 按钮名
            /// </summary>
            public string BtnName { get; set; }
            /// <summary>
            /// 上传到的地址
            /// </summary>
            public string Url { get; set; }
            /// <summary>
            /// 图片宽
            /// </summary>
            public int Width { get; set; }
            /// <summary>
            /// 图片高
            /// </summary>
            public int Height { get; set; }
            /// <summary>
            /// 是否显示路径控件
            /// </summary>
            public bool ShowPathCtrl { get; set; }

            /// <summary>
            /// 图片上传模型
            /// </summary>
            public Model()
            {
                this.Ext = "*.jpg;*.png;*.bmp;*.gif";
                this.SizeLimit = 1;
                this.BtnName = "选择图片";
                this.Url = "/fileupload/saveimage";
                this.ShowPathCtrl = true;
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public ActionResult Image(Model model)
        {
            return View("Index", model);
        }

        /// <summary>
        /// 上传文档或应该程序
        /// </summary>

        public ActionResult App(Model model)
        {
            return View("Index", model);
        }

        /// <summary>
        /// 帮助例子
        /// </summary>
        /// <returns></returns>
        public ActionResult Help()
        {
            return View();
        }

        /// <summary>
        /// 保存临时图片
        /// </summary>
        /// <returns></returns>
        [ResExtension("*.jpg;*.png;*.bmp;*.gif")]
        [ResFormat("*.jpg;*.png;*.bmp;*.gif")]
        public ActionResult SaveImage(int width = 0, int height = 0)
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];
                var res = ResManage.SavePostedFile(file, ResType.Temp, Guid.NewGuid());
                using (var image = ImageRes.Parse(res.FullFileName))
                {
                    var size = new Size(width, height);
                    if (size.IsEmpty == false)
                    {
                        image.SacleTo(size, ImageRes.SacleMode.Fill_XY);
                        image.Save();
                    }
                }
                return Content(res.UrlName);
            }
            return new EmptyResult();
        }

        /// <summary>
        /// 保存临时应用程序或文档
        /// </summary>
        /// <returns></returns>
        public ActionResult SaveApp()
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];
                var res = ResManage.SavePostedFile(file, ResType.Temp, Guid.NewGuid(), true, true);
                return Content(res.UrlName);
            }
            return new EmptyResult();
        }
    }
}
